package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseOilCardService;
import com.cloudlead.commonData.service.TbFuelRechargeService;
import com.cloudlead.commonData.vo.BaseOilCard;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.commonData.vo.TbFuelRecharge;
import com.cloudlead.fleetAction.service.FuelRechargeSer;
import com.cloudlead.fleetService.vo.FuelRechargeVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/12.
 */
@Service
public class FuelRechargeSerImpl implements FuelRechargeSer {
    @Autowired
    TbFuelRechargeService tbFuelRechargeService;
    @Autowired
    BaseOilCardService baseOilCardService;

    @Override
    public Page<FuelRechargeVo> findAllFuelRecharge(FuelRechargeVo vo, int page, int rows) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        List<Term> tr_1=new ArrayList<Term>();
        //查询条件
        tr.add(new Term("flag","0"));//未删除

        if (vo.getStartDate()!=null&&!"".equals(vo.getStartDate()))//充值起始日期
            tr.add(new Term("payDate",Operator.GE,vo.getStartDate()));

        if (vo.getDesetDate()!=null&&!"".equals(vo.getDesetDate()))//充值最后日期
            tr.add(new Term("payDate",Operator.LE,vo.getDesetDate()));

        tr_1.add(new Term("baseCompany.id",vo.getBaseCompany()));//公司Id
        if(vo.getType()!=null&&!"".equals(vo.getType()))//油卡类型
            tr_1.add(new Term("type",vo.getType()));
        if (vo.getOilCardNumber()!=null&&!"".equals(vo.getOilCardNumber()))//油卡编号
            tr_1.add(new Term("accountNumber",Operator.LK,vo.getOilCardNumber()));
        //查询符合条件的油卡
        List<BaseOilCard> baseOilCards=baseOilCardService.list(new Filter(new Criteria(tr_1)),new Sort());
        List baseOilCardsNew = new ArrayList();
        for(BaseOilCard baseOilCard: baseOilCards){
            baseOilCardsNew.add(baseOilCard.getId());
        }

        //查询的油卡记录
        if (baseOilCardsNew.size()>0){

           tr.add ((new Term("baseOilCard.id",Operator.IN,baseOilCardsNew)));
        }


        Filter filter=new Filter( new Criteria( tr));
        Sort sort = new Sort();
        sort.add(new Sort.Order( Sort.Direction.DESC ,"payDate"));



        pr.setSort(sort);
        pr.setSize(rows);
        pr.setPage(page);
        pr.setFilter(filter);


        Page<TbFuelRecharge> pb=(Page<TbFuelRecharge>)tbFuelRechargeService.findAll(pr);
        Page<FuelRechargeVo> pb1=new Page<FuelRechargeVo>();
        BeanUtils.copyProperties(pb, pb1);
        return pb1;
    }

    @Override
    public void createFuelRecharge(FuelRechargeVo vo) {
        TbFuelRecharge boc = new TbFuelRecharge();
        BeanUtils.copyProperties(vo, boc);
        boc.setPayDate(new DateTime());//充值日期

        if(vo.getBaseOilCard()!=null&&!"".equals(vo.getBaseOilCard())){
            BaseOilCard bo = baseOilCardService.view(vo.getBaseOilCard());
            bo.setId(vo.getBaseOilCard());
            boc.setBaseOilCard(bo);
            if ("副卡".equals(vo.getType())){//副卡充值
                System.out.println(vo.getBaseOilCard()+"====================" + vo.getNumber());
                BaseOilCard mainBo = baseOilCardService.view(vo.getNumber());
                if (bo.getEndMoney()==null) bo.setEndMoney((float)0.00);
                bo.setEndMoney(bo.getEndMoney()+vo.getRechargeAmount());//副卡卡的当前金额+充值金额
                mainBo.setEndMoney(mainBo.getEndMoney()-vo.getRechargeAmount());//主卡的当前金额-充值金额
                baseOilCardService.update(mainBo);//更新主卡当前金额
                baseOilCardService.update(bo);//更新副卡当前金额
            }else if ("主卡".equals(vo.getType())){//主卡充值
                if (bo.getEndMoney()==null) bo.setEndMoney((float)0.00);
                bo.setEndMoney(bo.getEndMoney()+vo.getRechargeAmount());//主卡的当前金额+充值金额
                baseOilCardService.update(bo);//更新主卡当前金额
            }



        }
        boc.setFlag("0");
        tbFuelRechargeService.create(boc);
    }

    /**
     * 撤销副卡充值
     * @param id
     */
    @Override
    public void editFuelRecharge(String id) {
        TbFuelRecharge boc =tbFuelRechargeService.view(id);
        boc.setFlag("1");
        tbFuelRechargeService.update(boc);
        BaseOilCard baseOilCard= boc.getBaseOilCard();//当前充值油卡

        //主卡
        BaseOilCard mainOilCard=baseOilCardService.findOne(new Filter(new Criteria(new Term("accountNumber",baseOilCard.getNumber()))));
        //把充值金额返还主卡
        baseOilCard.setEndMoney(baseOilCard.getEndMoney() - boc.getRechargeAmount());//副卡金额撤销
        mainOilCard.setEndMoney(mainOilCard.getEndMoney() + boc.getRechargeAmount());//主卡金额返回

        baseOilCardService.update(baseOilCard);
        baseOilCardService.update(mainOilCard);


    }

    /**
     * 撤销主卡充值
     * @param vo
     */
    @Override
    public void editMainFuelRecharge(FuelRechargeVo vo) {
        TbFuelRecharge boc = tbFuelRechargeService.view(vo.getId());
        BeanUtils.copyProperties(vo, boc);
        boc.setPayDate(new DateTime());//充值日期
        BaseOilCard bo = boc.getBaseOilCard();
        //撤销主卡充值金额
        bo.setEndMoney(bo.getEndMoney() - vo.getRechargeAmount());//主卡的当前金额-撤销金额
        baseOilCardService.update(bo);//更新主卡当前金额
        boc.setId(null);
        boc.setFlag("0");
        boc.setRechargeAmount(-vo.getRechargeAmount());//撤销金额为负数
        tbFuelRechargeService.create(boc);//新增负数充值记录
    }
}
